package com.century.sjt.EMChat.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.century.sjt.R;

/**
 * Created by 炳基 on 2016/7/7.
 * EaseChatTitleBar
 */
public class EaseChatTitleBar extends RelativeLayout {
    protected LinearLayout leftLayout;
    protected ImageView leftImage;
    protected LinearLayout rightLayout;
    protected ImageView rightImage1,rightImage2,rightImage3;
    protected TextView titleView;
    protected LinearLayout titleLayout;
    protected LinearLayout rightLayout1,rightLayout2,rightLayout3;

    public EaseChatTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public EaseChatTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EaseChatTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.sjt_ease_widget_title_bar, this);
        leftLayout = (LinearLayout) findViewById(R.id.left_layout);
        leftImage = (ImageView) findViewById(R.id.left_image);
        rightLayout = (LinearLayout) findViewById(R.id.right_layout);
        rightImage1 = (ImageView) findViewById(R.id.right_image1);
        rightImage2 = (ImageView) findViewById(R.id.right_image2);
        rightImage3 = (ImageView) findViewById(R.id.right_image3);
        rightLayout1 = (LinearLayout) findViewById(R.id.right_layout1);
        rightLayout2 = (LinearLayout) findViewById(R.id.right_layout2);
        rightLayout3 = (LinearLayout) findViewById(R.id.right_layout3);
        titleView = (TextView) findViewById(R.id.title);
        titleLayout = (LinearLayout) findViewById(R.id.root);


        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EaseChatTitleBar);
            String title = ta.getString(R.styleable.EaseChatTitleBar_ChattitleBarTitle);
            titleView.setText(title);

            Drawable leftDrawable = ta.getDrawable(R.styleable.EaseChatTitleBar_ChattitleBarLeftImage);
            if (null != leftDrawable) {
                leftImage.setImageDrawable(leftDrawable);
            }
            Drawable rightDrawable1 = ta.getDrawable(R.styleable.EaseChatTitleBar_ChattitleBarRightImage1);
            if (null != rightDrawable1) {
                rightImage1.setImageDrawable(rightDrawable1);
            }
            Drawable rightDrawable2 = ta.getDrawable(R.styleable.EaseChatTitleBar_ChattitleBarRightImage2);
            if (null != rightDrawable2) {
                rightImage2.setImageDrawable(rightDrawable2);
            }
            Drawable rightDrawable3 = ta.getDrawable(R.styleable.EaseChatTitleBar_ChattitleBarRightImage3);
            if (null != rightDrawable3) {
                rightImage3.setImageDrawable(rightDrawable3);
            }

            Drawable background = ta.getDrawable(R.styleable.EaseChatTitleBar_ChattitleBarBackground);
            if(null != background) {
                titleLayout.setBackgroundDrawable(background);
            }

            ta.recycle();
        }
    }

    public void setLeftImageResource(int resId) {
        leftImage.setImageResource(resId);
    }

    public void setRightImageResource1(int resId) {
        rightImage1.setImageResource(resId);
    }
    public void setRightImageResource2(int resId) {
        rightImage2.setImageResource(resId);
    }
    public void setRightImageResource3(int resId) {
        rightImage3.setImageResource(resId);
    }

    public void setLeftLayoutClickListener(OnClickListener listener){
        leftLayout.setOnClickListener(listener);
    }

    public void setRightLayoutClickListener(OnClickListener listener){
        rightLayout.setOnClickListener(listener);
    }
    public void setRightLayout1_ClickListener(OnClickListener listener){
        rightLayout1.setOnClickListener(listener);
    }
    public void setRightLayout2_ClickListener(OnClickListener listener){
        rightLayout2.setOnClickListener(listener);
    }
    public void setRightLayout3_ClickListener(OnClickListener listener){
        rightLayout3.setOnClickListener(listener);
    }


    public void setRightLayoutVisibility(int visibility){
        rightLayout.setVisibility(visibility);
    }
    public void setRightLayout2Visibility(int visibility){
        rightLayout2.setVisibility(visibility);
    }
    public void setRightLayout3Visibility(int visibility){
        rightLayout3.setVisibility(visibility);
    }

    public void setTitle(String title){
        titleView.setText(title);
    }

    public void setBackgroundColor(int color){
        titleLayout.setBackgroundColor(color);
    }

    public LinearLayout getLeftLayout(){
        return leftLayout;
    }

    public LinearLayout getRightLayout(){
        return rightLayout;
    }
}
